package com.sskj.givecoin.v.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.sskj.givecoin.R;
import com.sskj.givecoin.common.App;
import com.sskj.givecoin.common.ClassicsHeader;
import com.sskj.givecoin.common.adapter.BaseAdapter;
import com.sskj.givecoin.common.adapter.ViewHolder;
import com.sskj.givecoin.common.widget.ToolBarLayout;
import com.sskj.givecoin.m.bean.CommonBean;
import com.sskj.givecoin.p.activity.ShareDetailPresenter;
import com.sskj.givecoin.utils.TimeFormatUtil;
import com.sskj.givecoin.v.base.BaseActivity;

import java.util.Date;

import butterknife.BindView;

/**
 * 1 原力明细
 * 2
 */
public class CommonDetail extends BaseActivity<ShareDetailPresenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.share_detail_list)
    RecyclerView shareDetailList;
    @BindView(R.id.share_refresh)
    SmartRefreshLayout shareRefresh;
    public int type;
    private int page = 1;
    private BaseAdapter<CommonBean.DataBean> adapter;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_share_detail;
    }

    @Override
    public ShareDetailPresenter getPresenter() {
        return new ShareDetailPresenter();
    }

    @Override
    protected void initView() {
        toolbar.setTitle(App.INSTANCE.getString(R.string.fabimineJavaCommonDetail2));
//        type = getIntent().getIntExtra("type", 0);
        toolbar.setLeftButtonOnClickLinster(v -> finish());
        shareRefresh.setRefreshHeader(new ClassicsHeader(this));
        shareRefresh.setRefreshFooter(new ClassicsFooter(this));
        shareRefresh.setOnRefreshListener(v -> {
            page = 1;
            getData();
        });
        shareRefresh.setOnLoadMoreListener(v -> {
            page++;
            getData();
        });
        shareDetailList.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseAdapter<CommonBean.DataBean>(R.layout.item_share, null, shareDetailList, true) {
            @Override
            public void bind(ViewHolder holder, CommonBean.DataBean item) {
                holder.setText(R.id.name, item.getMemo());
//                if (type != 5) {
//                } else {
//                    switch (item.getState()) {
//                        case 0:
//                            holder.setText(R.id.name, App.INSTANCE.getString(R.string.fabimineJavaCommonDetail4));
//                            break;
//                        case 1:
//                            holder.setText(R.id.name, App.INSTANCE.getString(R.string.fabimineJavaCommonDetail5));
//                            break;
//                        case 2:
//                            holder.setText(R.id.name, App.INSTANCE.getString(R.string.fabimineJavaCommonDetail6));
//                            break;
//                        case 3:
//                            holder.setText(R.id.name, App.INSTANCE.getString(R.string.fabimineJavaCommonDetail7));
//                            break;
//                    }
//                }

                holder.setText(R.id.account, item.getPrice());
//                double time = Double.valueOf(TimeFormatUtil.parseLongE(item.getAddtime())) * 1000;
                holder.setText(R.id.time, TimeFormatUtil.SF_FORMAT_B.format(new Date(Long.valueOf(item.getAddtime()) * 1000)));
            }
        };

    }

    @Override
    protected void initData() {
        getData();
    }


    public void setData(CommonBean data) {
        if (page == 1) {
            adapter.setNewData(data.getRes());
        } else {
            adapter.addData(data.getRes());
        }
    }

    public void stopRefresh() {
        shareRefresh.finishRefresh();
        shareRefresh.finishLoadMore();
    }

    public void getData() {
        mPresenter.getDataDetail(page);
    }


    public static void start(Context context, int type) {
        Intent intent = new Intent(context, CommonDetail.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }
}
